﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using umbraco;
using umbraco.cms.businesslogic.macro;
using System.IO;

namespace Nibble.Umb.Locator.Installer
{
    public class AddNewSchemaContentToFile : umbraco.interfaces.IPackageAction

    {
        public string Alias()
        {
            return "addNewSchemaContentToFile";
        }

        public bool Execute(string packageName, System.Xml.XmlNode xmlData)
        {
            string basePath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
            string sourceFile = xmlHelper.GetNodeValue(xmlData.SelectSingleNode("source"));
            string targetFile = xmlHelper.GetNodeValue(xmlData.SelectSingleNode("target"));

            try
            {
                if (!Utility.UseLegacySchema)
                {
                    if (File.Exists(basePath + sourceFile))
                    {
                        if (File.Exists(basePath + targetFile))
                            File.Delete(basePath + targetFile);

                        File.Move(basePath + sourceFile, basePath + targetFile);
                    }

                    return true;
                }
                else
                {
                    File.Delete(basePath + sourceFile);
                    return true;
                }
            }
            catch(MissingMemberException)
            {
                File.Delete(basePath + sourceFile);
                return true;
            }

        }

        public System.Xml.XmlNode SampleXml()
        {
            throw new NotImplementedException();
        }

        public bool Undo(string packageName, System.Xml.XmlNode xmlData)
        {
            throw new NotImplementedException();
        }


    }
}